package com.example.androidlearning.sy4;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidlearning.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListView02 extends AppCompatActivity {

    private String[] names;
    private String[] buttons;
    private int[] images;

    private ListView listView;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simpleadapterlv);

        init();
        setListeners();

        registerForContextMenu(textView);//为mTv注册事件
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,1,0,"菜单一");
        menu.add(0,2,0,"菜单二");
        menu.add(0,3,0,"菜单三");
        menu.add(0,4,0,"菜单四");
        SubMenu subMenu = menu.addSubMenu("子菜单");
        subMenu.add(0,5,0, "子菜单一");
        subMenu.add(0,6,0, "子菜单二");
        subMenu.add(0,7,0, "子菜单三");
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        if(v == textView){
            menu.add(0, 1, 0, "上下文菜单一");
            menu.add(0, 2, 0, "上下文菜单二");
            menu.add(0, 3, 0, "上下文菜单三");
        }
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    void init(){
        names = new String[]{"蛋糕","礼物", "邮票", "爱心"};
        buttons = new String[]{"商品详情","商品详情", "商品详情", "商品详情"};
        images = new int[]{R.mipmap.richu_riluo_004,R.mipmap.richu_riluo_004,R.mipmap.richu_riluo_004,R.mipmap.richu_riluo_004};
        listView = findViewById(R.id.listview_sa);
        textView = findViewById(R.id.item_text);

        List<Map<String,Object>> listItems = new ArrayList<>();
        for(int i = 0; i < names.length; i++){
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("headers", images[i]);
            listItem.put("names", names[i]);
            listItem.put("buttons", buttons[i]);
            listItems.add(listItem);
        }

        MySimpleAdapter simpleAdapter = new MySimpleAdapter(this, listItems, R.layout.simple_item, new String[]{"headers","names","buttons"}, new int[]{R.id.header, R.id.name, R.id.item_button});
        listView.setAdapter(simpleAdapter);
    }

    void setListeners(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                System.out.println("s");
                showNormalDialog(position);
            }
        });
    }

    private void showNormalDialog(int i){
        //创建dialog构造器
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        //设置title
        normalDialog.setTitle(names[i]);
        //设置icon
        normalDialog.setIcon(R.mipmap.ic_launcher_round);
        //设置内容
        normalDialog.setMessage(names[i] + "：好好吃");
        //设置按钮
        normalDialog.setPositiveButton("确定"
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        //创建并显示
        normalDialog.create().show();
    }


}
